package ${integrationPackageName}.impl;

import com.bifang.common.annotations.ApiService;
import com.bifang.common.annotations.OpApi;
import com.bifang.common.annotations.SaveOplog;
import com.bifang.common.constant.enums.common.AuTypeEnum;
import com.bifang.common.constant.enums.common.FuncTypeEnum;
import com.bifang.common.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;
import java.util.List;
import com.bifang.common.exception.AppException;
import org.springframework.util.ObjectUtils;
import com.bifang.common.annotations.NoRepeatSubmit;
import ${VO}.${entity}VO;
import ${KeyVO}.${entity}KeyVO;
import ${SaveVO}.${entity}SaveVO;
import ${integrationPackageName}.${entity}Integration;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
/**
 * ${table.comment!}业务实现类
 *
 * @author ${author}
 * @since ${date}
 */
@ApiService(funcCode = "${funcCode}", title = "${table.comment!}业务实现")
public class ${entity}IntegrationImpl implements ${entity}Integration {

    @Autowired
    public ${table.serviceName} ${table.entityPath}Service;

    @Override
    @OpApi(funcCode = "${funcCode}0001", title = "${table.comment!}分页查询", funcType = FuncTypeEnum.query)
    public PageInfo${"<"}${entity}VO${">"} getList(${entity}VO vo) {
        // 开启分页
        if (vo.getPageNum() != null && vo.getPageSize() != null) {
            PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        }
        List${"<"}${entity}${">"} list = ${table.entityPath}Service.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo${"<"}${entity}${">"} pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List${"<"}${entity}VO${">"} resultList = Convert.toList(${entity}VO.class, pageInfo.getList());
        // 转义数据字典值
        // dictUtil.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "${funcCode}0002", title = "${table.comment!}根据id查询", funcType = FuncTypeEnum.query)
    public ${entity}VO getById(${entity}KeyVO vo) {
        ${entity} entity = ${table.entityPath}Service.getById(vo.${getId}());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到${table.comment!}信息!");
        }
        ${entity}VO result = Convert.convert(${entity}VO.class, entity);
        return result;
    }

    @Override
    @NoRepeatSubmit
    @OpApi(funcCode = "${funcCode}0003", title = "${table.comment!}新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "${funcCode}0003",
            operationName = "${table.comment!}新增",
            dataType = "${id}",
            auType = AuTypeEnum.OTHER)
    public void save(${entity}SaveVO vo) {
        ${entity} entity = Convert.convert(${entity}.class, vo);
        ${table.entityPath}Service.save(entity);
    }

    @Override
    @NoRepeatSubmit
    @OpApi(funcCode = "${funcCode}0004", title = "${table.comment!}修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "${funcCode}0004",
            operationName = "${table.comment!}修改",
            dataType = "${id}",
            auType = AuTypeEnum.OTHER)
    public void updateById(${entity}SaveVO vo) {
        if (ObjectUtils.isEmpty(vo.${getId}())) {
            throw new AppException("${table.comment!}主键不能为空!");
        }
        ${entity} entity = Convert.convert(${entity}.class, vo);
        ${table.entityPath}Service.updateById(entity);
    }

    @Override
    @NoRepeatSubmit
    @OpApi(funcCode = "${funcCode}0005", title = "${table.comment!}删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "${funcCode}0005",
            operationName = "${table.comment!}删除",
            dataType = "${id}",
            auType = AuTypeEnum.OTHER)
    public void removeById(${entity}KeyVO vo) {
        if (ObjectUtils.isEmpty(vo.${getId}())) {
            throw new AppException("${table.comment!}主键不能为空!");
        }
        ${entity} entity = ${table.entityPath}Service.getById(vo.${getId}());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到${table.comment!}要删除的信息!");
        }
        ${table.entityPath}Service.removeById(vo.${getId}());
    }
}
